derive-where
Description
Attribute proc-macro to simplify deriving standard and other traits with custom generic type bounds.
Usage
The derive_where
attribute can be used just like
std's #[derive(...)]
statements:
;
This will generate trait implementations for Example
for any T
,
as opposed to std's derives, which would only implement these traits with
T: Trait
bound to the corresponding trait.
Multiple derive_where
attributes can be added to an
item, but only the first one must use any path qualifications.
;
If using a different package name, you must specify this:
;
In addition, the following convenience options are available:
Generic type bounds
Separated from the list of traits with a semi-colon, types to bind to can be
specified. This example will restrict the implementation for Example
to
T: Clone
:
;
It is also possible to specify the bounds to be applied. This will
bind implementation for Example
to T: Super
:
;
But more complex trait bounds are possible as well.
The example below will restrict the Clone
implementation for Example
to T::Type: Clone
:
;
;
Any combination of options listed here can be used to satisfy a specific constrain. It is also possible to use multiple separate constrain specifications when required:
;
Enum default
Since Rust 1.62 deriving Default
on an enum is possible with the
#[default]
attribute. Derive-where allows this with a
#[derive_where(default)]
attribute:
Skipping fields
With a skip
or skip_inner
attribute fields can be skipped for traits
that allow it, which are: Debug
, Hash
, Ord
, PartialOrd
,
PartialEq
, Zeroize
and ZeroizeOnDrop
.
;
assert_eq!;
assert_eq!;
] T)
It is also possible to skip all fields in an item or variant if desired:
;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Selective skipping of fields for certain traits is also an option, both in
skip
and skip_inner
. To prevent breaking invariants defined for these
traits, some of them can only be skipped in groups. The following groups are
available:
Debug
EqHashOrd
: SkipsEq
,Hash
,Ord
,PartialOrd
andPartialEq
.Hash
Zeroize
: SkipsZeroize
andZeroizeOnDrop
.
;
assert_eq!;
assert_ne!;
Incomparable variants/items
Similar to the skip
attribute, incomparable
can be used to skip variants
or items in PartialEq
and PartialOrd
trait implementations, meaning
they will always yield false
for eq
and None
for partial_cmp
. This
results in all comparisons but !=
, i.e. ==
, <
, <=
, >=
and >
,
with the marked variant or struct evaluating to false
.
# use derive_where;
assert_eq!;
assert_ne!;
assert!;
assert!;
assert!;
assert!;
;
assert_ne!;
assert!;
assert!;
Note that it is not possible to use incomparable
with Eq
or Ord
as
that would break their invariants.
Zeroize
options
Zeroize
has two options:
crate
: an item-level option which specifies a path to thezeroize
crate in case of a re-export or rename.fqs
: a field-level option which will use fully-qualified-syntax instead of calling thezeroize
method onself
directly. This is to avoid ambiguity between another method also calledzeroize
.
i32);
let mut test = Example;
// Will call the struct method.
test.zeroize;
assert_eq!;
// WIll call the `Zeroize::zeroize` method.
zeroize;
assert_eq!;
)]
ZeroizeOnDrop
options
If the zeroize-on-drop
feature is enabled, it implements ZeroizeOnDrop
and can be implemented without Zeroize
, otherwise it only implements
Drop
and requires Zeroize
to be implemented.
ZeroizeOnDrop
has one option:
crate
: an item-level option which specifies a path to thezeroize
crate in case of a re-export or rename.
;
assert!;
Supported traits
The following traits can be derived with derive-where:
Clone
Copy
Debug
Default
Eq
Hash
Ord
PartialEq
PartialOrd
Zeroize
: Only available with thezeroize
crate feature.ZeroizeOnDrop
: Only available with thezeroize
crate feature. If thezeroize-on-drop
feature is enabled, it implementsZeroizeOnDrop
, otherwise it only implementsDrop
.
Supported items
Structs, tuple structs, unions and enums are supported. Derive-where tries
it's best to discourage usage that could be covered by std's derive
. For
example unit structs and enums only containing unit variants aren't
supported.
Unions only support Clone
and Copy
.
PartialOrd
and Ord
need to determine the discriminant type to
function correctly. To protect against a potential future change to the
default discriminant type, some compile-time validation is inserted to
ascertain that the type remains isize
.
no_std
support
no_std
support is provided by default.
Crate features
nightly
: ImplementsOrd
andPartialOrd
with the help ofcore::intrinsics::discriminant_value
, which is what Rust does by default too. This requires a nightly version of the Rust compiler.safe
:safe
: Uses only safe ways to access the discriminant of the enum forOrd
andPartialOrd
. It also replaces all cases ofcore::hint::unreachable_unchecked
inOrd
,PartialEq
andPartialOrd
, which is what std uses, withunreachable
.zeroize
: Allows derivingZeroize
andzeroize
onDrop
.zeroize-on-drop
: Allows derivingZeroize
andZeroizeOnDrop
and requireszeroize
v1.5.
MSRV
The current MSRV is 1.57 and is being checked by the CI. A change will be
accompanied by a minor version bump. If MSRV is important to you, use
derive-where = "~1.x"
to pin a specific minor version to your crate.
Alternatives
- derivative
is a great alternative with many options. Notably it doesn't support
no_std
and requires an extra#[derive(Derivative)]
to use. - derive_bounded is a new alternative still in development.
Changelog
See the CHANGELOG file for details.
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.